<?php

namespace App\HttpController\Applet\Yuanyang;


use App\Model\WechatsModel;
use App\Utility\Cache;
use App\Utility\Music;
use Carbon\Carbon;
use EasySwoole\EasySwoole\Logger;


class IndexController extends AuthController {
    //列表
    public function index(){
        $request = $this->request();
        $params = $request->getRequestParam();
        $appletModel = $this->appletArticleModel($this->applet);
        $model = new $appletModel;
        $list = $model->select('id,thumb,title,is_link,link_url')
            ->where(function ($query) use($params){
                if(isset($params['hot']) && $params['hot']) $query->where('hot',$params['hot']);
            })
            ->where('status',1)
            ->inRandomOrder()
            ->take(40)
            ->get();
        return $this->writeJson(0,$list);
    }
    //详情
    public function info(){
        $request = $this->request();
        $id = $request->getRequestParam('id') ??  0;
        $share = $request->getRequestParam('share') ?? null;
        $pramas = $request->getRequestParam() ;
        if(isset($pramas['test'])){
            Logger::getInstance()->log(json_encode($pramas).PHP_EOL,'info-yuanyang-test');
        }
//        Logger::getInstance()->log(json_encode($pramas).PHP_EOL,'info-yuanyang-test');
        $openid = $request->getRequestParam('openid') ?? '';

        if(empty($id)){
            return $this->writeJson(1,null,'参数不正确');
        }
        $appletModel = $this->appletArticleModel($this->applet);
        $model = new $appletModel;
        $columns = 'articles_'.$this->applet.'.id,cateid,userid,title,title_color,articles_'.$this->applet.'.thumb,music,articles_'.$this->applet.'.created_at,articles_'.$this->applet.'.is_link,articles_'.$this->applet.'.link_url,content,backimg,a.name as username,a.thumb as avatar';
        $info = $model->select($columns)
            ->join('`categorys` as a', 'a.id = articles_'.$this->applet.'.cateid','RIGHT')
            ->find($id);
        if(empty($info)){
            return $this->writeJson(1,null,'抱歉，数据不存在！');
        }
        $queueData = [
            'id' => $id,
            'admin_id' => $info['userid'],
            'share' => $share,
            'openid' => $openid,
            'applet' => $this->applet,
            'applet_type' => 'article',
        ];
        $info['music'] = (new Music())->getMusic($info['music']);

        //开启任务-创建统计自增推送队列
        $queue = new Cache();
        $queue->onQueue('count-inc');
        $queue->push($queueData);
        return $this->writeJson(0,$info);
    }

    //随机查询8条文章
    public function list(){
        $appletModel = $this->appletArticleModel($this->applet);
        $model = new $appletModel;
        $list = $model->select('id,thumb,title,is_link,link_url')
            ->where('status',1)
            ->inRandomOrder()
            ->take(8)
            ->get();
        return $this->writeJson(0,$list);
    }


    //收集模板消息的formid
    public function formId(){
        try{
            $request = $this->request();
            $data = $request->getRequestParam();
            if($data['formid'] =='the formId is a mock one'){
                return $this->writeJson(1,null,'测试数据');
            }
            if(!isset($data['openid']) || !$data['openid']){
                return $this->writeJson(1,null,'参数错误');
            }

//            Logger::getInstance()->log('FORMID:\n'.json_encode($data),'formid-yuanyang-test');
            $queue = new Cache();
            $queue->onQueue('formid');
            $queue->push([
                'openid' => $data['openid'],
                'expire' => Carbon::now()->addDays(7)->timestamp,
                'formid' => $data['formid'],
                'name'   => $this->applet
            ]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    public function config(){
        $request = $this->request();
        $name= $request->getRequestParam('name') ?? '';
        if(!$name){
            return $this->writeJson(1,null,'参数不正确');
        }
        $appletModel = $this->appletConfigModel($this->applet);
        $model = new $appletModel;
        $list = $model->where('name',$name)->first();
        return $this->writeJson(0,$list);
    }
    /**
     * 获取公众号链接文章
     */
    public function wechatLink(){
        $request = $this->request();
        $position = $request->getRequestParam('position') ?? 1;
        $appletModel = new WechatsModel();
        $list = $appletModel->where('position',$position)->where('name',$this->applet)->get();
        return $this->writeJson(0,$list);
    }
}
